package com.qf.zyh.services.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.zyh.common.ResponseResult;
import com.qf.zyh.common.ResultType;
import com.qf.zyh.dao.RoleMapper;
import com.qf.zyh.dao.RoleMenuMapper;
import com.qf.zyh.entity.Role;
import com.qf.zyh.services.RoleServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServicesImpl implements RoleServices {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Override
    public List<Integer> getRoleMenu(Integer roleId) {
        List<Integer> integerList =roleMapper.getRoleMenu(roleId);
        return integerList;
    }

    @Override
    public ResultType getRoleByPage(Integer page, Integer limit, String name) {
        PageHelper.startPage(page,limit);
        List<Role> roleList=roleMapper.getRoleWithName(name);
        PageInfo pi = new PageInfo(roleList);
        return new ResultType(0,"",(int)pi.getTotal(),pi.getList());
    }

    @Override
    public ResponseResult updaStatusByid(Integer id, Integer status) {
        if (id>0) {
            Role role = new Role(id,status);
            int i = roleMapper.updateByPrimaryKeySelective(role);
            if (i>0) {
                return ResponseResult.getSuccess("修改成功咯");
            }
        }
        return ResponseResult.getFail("修改失败嗷");
    }

    @Override
    public ResponseResult updaMenu(Integer roleId, Integer[] menuIds) {
        roleMenuMapper.deleteByPrimaryrid(roleId);
        roleMenuMapper.insertrolement(roleId,menuIds);
        return ResponseResult.getSuccess();
    }

    @Override
    public ResponseResult updaRole(Role l) {
        int i = roleMapper.updateByPrimaryKeySelective(l);
        if (i>0) {
            return ResponseResult.getSuccess();
        }
        return ResponseResult.getFail("出错");
    }

    @Override
    public ResponseResult addRole(Role l) {
        int i = roleMapper.insertSelective(l);
        if (i > 0) {
            return ResponseResult.getSuccess();
        }
        return ResponseResult.getFail("出错");
    }

    @Override
    public List<Role> getRole() {
        return roleMapper.getRole();
    }
}
